package com.invengo.module_check.ui.fragment.widget;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.invengo.library_rfid.RFIDTestUtils;
import com.invengo.library_rfid.analyze.EPCAnalyzeFactory;
import com.invengo.library_rfid.analyze._EpcRuleTag;
import com.invengo.library_rfid.classify.AnalyzeImpl;
import com.invengo.library_rfid.classify.RFIDCallBackListener;
import com.invengo.library_rfid.classify.RfidModelImpl;
import com.invengo.library_rfid.entity.TagInfo;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.repsitory.business.BusinessRepsitory;
import com.invengo.module_check.ui.fragment.widget.item.AnalyzeItemViewModel;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class AnalyzeTestViewModel extends BaseViewModel<BusinessRepsitory> {
    public SingleLiveEvent<Boolean> isBookType = new SingleLiveEvent<>();


    public ObservableList<AnalyzeItemViewModel> itemList = new ObservableArrayList<>();
    public ItemBinding<AnalyzeItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_dialog_analyze_value);

    public AnalyzeTestViewModel(@NonNull Application application) {
        super(application, BusinessRepsitory.getInstance());
        registerRFID(new RFIDCallBackListener(AnalyzeImpl.TO_BOOK, tagInfo -> onScanResult(tagInfo), state -> isReading.set(state)));
        isBookType.postValue(false);
    }

    public void onScanResult(TagInfo tagInfo) {
        Observable.just(tagInfo)
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(tagInfo1 -> itemList.add(new AnalyzeItemViewModel(this, tagInfo1)));
    }


    public void onTypeChange(boolean b) {
        isBookType.postValue(b);
        RxBus.getDefault().postSticky(b ? AnalyzeImpl.TO_SHELF : AnalyzeImpl.TO_BOOK);
    }

    public int getPosition(ItemViewModel item) {
        return itemList.indexOf(item);
    }

    /**
     * 清空列表
     */
    public BindingCommand cleanByBookCommand = new BindingCommand(() -> {
        cleanRFIDCache();
        itemList.clear();
    });
    /**
     * 清空列表
     */
    public BindingCommand testCodeData = new BindingCommand(() -> {
//        if (isBookType.getValue()) {
//            RFIDTestUtils.testShelfs();
//        }else{
//            RFIDTestUtils.testBooks();
//        }
    });
}
